/**
 * 
 */
package org.swing.utility.common.jmx;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.DynamicMBean;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;

/**
 * @author lqnhu
 *
 */
public class MapMBean implements DynamicMBean {
	private Map<String, ?> map;

	public MapMBean(Map<String, ?> map) {
		setMap(map);
	}

	public void setMap(Map<String, ?> map) {
		if (map == null) {
			this.map = new HashMap<String, Object>();
		} else {
			this.map = map;
		}
	}

	public Object getAttribute(String attribute) {
		return map.get(attribute);
	}

	public AttributeList getAttributes(String[] attributes) {
		AttributeList attrLst = new AttributeList(map.size());
		for (String key : attributes) {
			if (map.containsKey(key)) {
				attrLst.add(new Attribute(key, map.get(key)));
			}
		}
		return attrLst;
	}

	public MBeanInfo getMBeanInfo() {
		List<MBeanAttributeInfo> aInfo = new ArrayList<MBeanAttributeInfo>();
		for (String key : map.keySet()) {
			aInfo.add(new MBeanAttributeInfo(key, "java.lang.String", "", true,
					false, false));
		}
		MBeanAttributeInfo[] aArray = aInfo
				.toArray(new MBeanAttributeInfo[aInfo.size()]);
		return new MBeanInfo("com.addthis.basis.jmx.MapMBean", null, aArray,
				null, null, null);
	}

	public Object invoke(String actionName, Object[] params, String[] signature) {
		return null; // Throw not implemented?
	}

	public void setAttribute(Attribute attribute) {
		// Throw not implemented?
	}

	public AttributeList setAttributes(AttributeList attributes) {
		return null; // Throw not implemented?
	}
}
